package org.spark.java.transformation;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.api.java.function.VoidFunction;
import scala.Tuple2;

import java.util.Arrays;
import java.util.List;

/**
 * 功能描述: 根据key计算和
 *
 * @param:
 * @return:
 * @auther: 某人的目光
 * @date: 2020/1/13 22:22
 */
public class ReduceByKey {

    public static void main(String[] args) {
        SparkConf sparkConf = new SparkConf();
        //设置运行环境
        sparkConf.setMaster("local");
        sparkConf.setAppName("reduceByKey");
        JavaSparkContext context = new JavaSparkContext(sparkConf);
        List<Tuple2<String, Integer>> list = Arrays.asList(
                new Tuple2<String, Integer>("南京", 70),
                new Tuple2<String, Integer>("安徽", 80),
                new Tuple2<String, Integer>("安徽", 100));
//        JavaPairRDD<String, Integer> listRdd = context.parallelizePairs(list);
        JavaRDD<Tuple2<String, Integer>> listRdd = context.parallelize(list);
        JavaPairRDD<String, Integer> mapToPair = listRdd.mapToPair(new PairFunction<Tuple2<String, Integer>, String, Integer>() {
            public Tuple2<String, Integer> call(Tuple2<String, Integer> t) throws Exception {
                return new Tuple2<String, Integer>(t._1, t._2);
            }
        });
        mapToPair.reduceByKey(new Function2<Integer, Integer, Integer>() {
            public Integer call(Integer v1, Integer v2) throws Exception {
                return v1 + v2;
            }
        }).foreach(new VoidFunction<Tuple2<String, Integer>>() {
            public void call(Tuple2<String, Integer> t) throws Exception {
                System.out.println(t._1 + ":" + t._2);
            }
        });
    }
}
